package com.gopay.bis.web.controller.ebank;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BocConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.boc.ebank.BocB2cMessage;

/**
 * @BocB2cServerRes.java
 * @author wanggang
 * @2013-11-7 下午2:53:01  www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class BocB2cServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog(BocB2cServerRes.class);

    @RequestMapping(value = "/BocB2CServerRes")
    protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
        logger.error("--------------- BocB2CServerRes bankback ---------------");
        if(request.getMethod().toUpperCase().equals("GET")){
        	logger.error("------GET -------");
        	// 打印请求参数
    		this.displayRequestParameters(request, logger);
    		return null;
        }else{
        	return super.execute(request, response);
        }
        
    }
	
	@Override
	protected BocB2cMessage buildMessage(HttpServletRequest request) {
		BocB2cMessage message = new BocB2cMessage();
        message.setBank(BankConstants.BANK_LOGO_BOC);
        message.setMerchantNo(request.getParameter("merchantNo"));
        message.setOrderId(request.getParameter("orderNo"));
        message.setBankOrderId(request.getParameter("orderNo"));
        message.setOrderSeq(request.getParameter("orderSeq"));
        message.setCardTyp(request.getParameter("cardTyp"));
        message.setPayTime(request.getParameter("payTime"));
        message.setOrderStatus(request.getParameter("orderStatus"));
        message.setPayAmount(request.getParameter("payAmount"));
        message.setSignMessage(request.getParameter("signData"));
        //银行业务产品01个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
        return message;
    }

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request,Result result) {
	    OrderInfo orderInfo = new OrderInfo();
	    String orderStatus = request.getParameter("orderStatus");
	    if(BocConstants.B2C_ORDER_STATUS_PAY.equals(orderStatus)){
	        orderInfo.setOrderId(request.getParameter("orderNo"));
	        orderInfo.setAmt(request.getParameter("payAmount"));
	        orderInfo.setNotifyNum("1");
	        orderInfo.setPayStatus("");
	        orderInfo.setRetCode("00");
	        orderInfo.setRetMsg("");
	    }else{
	        orderInfo.setOrderId(request.getParameter("orderNo"));
	        orderInfo.setAmt(request.getParameter("payAmount"));
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			orderInfo.setRetMsg("");
	    }	
        return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("merchantNo"))
				|| StringUtils.isEmpty(request.getParameter("orderNo"))
				|| StringUtils.isEmpty(request.getParameter("payAmount"))
				|| StringUtils.isEmpty(request.getParameter("orderStatus"))
				|| StringUtils.isEmpty(request.getParameter("signData"))) {
		return false;
		}
		return true;
	}

}
